/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.tmodel.NursingPlanServiceItem;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository; 

@JsonApiResourceRepository(NursingPlanServiceItem.class)
@Component
public class NursingPlanItemRepository extends BaseRepository<NursingPlanServiceItem> {

	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() { 
		dslJpaService.setEntityClass(NursingPlanServiceItem.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<NursingPlanServiceItem> findAll(final QueryParams requestParams) {
		String action = ActionContextHolder.getContext().getAction();
		ArrayList<NursingPlanServiceItem> itemList = new ArrayList<NursingPlanServiceItem>();
		if ("spec_planService".equals(action)) {
			long uid = -1;
			Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
			Iterator iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				Map.Entry entry = (Map.Entry) iter.next();
				String key = (String) entry.getKey();
				String val = (String) entry.getValue();
				if ("uid".equals(key)) {
					uid = Long.parseLong(val);
					break;
				}
			}// uid=map.get("uid")
			if (uid != -1)
				return nursingService.getStaffPlanItem(uid);
			else
				return itemList;
		} else {
		return super.findAll(requestParams);
		}
	}

}
